// Ionic Starter App
//定义变量
//var BASE_URL = 'http://app.yigeqinhang.com/';
//var BASE_URL = 'http://manage.yigeqinhang.com/';
var BASE_URL = 'http://localhost:8002/';
var userAuth = {};

//app路由设置
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])

  .run(function ($ionicPlatform) {
    $ionicPlatform.ready(function () {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        cordova.plugins.Keyboard.disableScroll(true);
      }
      if (window.StatusBar) {
        // org.apache.cordova.statusbar required
        StatusBar.styleDefault();
      }
    });
  })

  .run(['$rootScope', '$location', '$state', '$http', '$ionicPopup', function ($rootScope, $location, $state, $http, $ionicPopup) {
    //var loginName = ['tab.member', 'loan', 'repayment', 'iftab.exe'];
    //$http.jsonp(BASE_URL + "home/appapi/isLogin?callback=JSON_CALLBACK")//??????????
    //  .success(function (data) {
    //    if (data['status'] == '1') {
    //      $rootScope.islogin = true;
    //    } else {
    //      $rootScope.islogin = false;
    //    }
    //  }).error(function () {
    //  $rootScope.islogin = false;
    //});

    //监听路由事件
    $rootScope.$on('$stateChangeStart',
      function (event, toState, toParams, fromState, fromParams) {
        if (!$rootScope.islogin) {
          $ionicPopup.alert({
            title: "提示",
            template: "用户请先登陆！",
            okText: "知道了"
          }).then(function () {
            $rootScope.islogin = true;
            $state.go("login");
            $rootScope.islogin = false
          });
        }
      })
  }])

  .config(function ($stateProvider, $urlRouterProvider, $ionicConfigProvider, $httpProvider) {

    $ionicConfigProvider.platform.ios.tabs.style('standard');
    $ionicConfigProvider.platform.ios.tabs.position('bottom');
    $ionicConfigProvider.platform.android.tabs.style('standard');
    $ionicConfigProvider.platform.android.tabs.position('standard');

    $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
    $ionicConfigProvider.platform.android.navBar.alignTitle('center');

    //$ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
    $ionicConfigProvider.platform.ios.backButton.previousTitleText('返回').icon('');
    //$ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');
    $ionicConfigProvider.platform.android.backButton.previousTitleText('返回').icon('');

    $ionicConfigProvider.platform.ios.views.transition('ios');
    $ionicConfigProvider.platform.android.views.transition('android');

    // Ionic uses AngularUI Router which uses the concept of states
    // Learn more here: https://github.com/angular-ui/ui-router
    // Set up the various states which the app can be in.
    // Each state's controller can be found in controllers.js
    $stateProvider

      .state('login', {
        url: '/login',
        templateUrl: 'templates/login.html',
        controller: 'userLoginCtrl'
      })

      // setup an abstract state for the tabs directive
      .state('tab', {
        url: '/tab',
        abstract: true,
        templateUrl: 'templates/tabs.html'
      })

      // Each tab has its own nav history stack:

      .state('tab.dash', {
        url: '/dash',
        views: {
          'tab-dash': {
            templateUrl: 'templates/tab-dash.html',
            controller: 'DashCtrl'
          }
        }
      })

      .state('tab.chats', {
        url: '/chats',
        views: {
          'tab-chats': {
            templateUrl: 'templates/tab-chats.html',
            controller: 'ChatsCtrl'
          }
        }
      })

      .state('tab.chat-stuCost', {
        url: '/chats/{chatId:2}',
        views: {
          'tab-chats': {
            templateUrl: 'templates/chat-detail.html',
            controller: 'ChatStuCostCtrl'
          }
        }
      })

      .state('tab.chat-teachLesson', {
        url: '/chats/{chatId:3}',
        views: {
          'tab-chats': {
            templateUrl: 'templates/chat-teachLesson.html',
            controller: 'chatTeachLessonCtrl'
          }
        }
      })

      .state('tab.chat-teacherLesson', {
        url: '/chats/{chatId:4}',
        views: {
          'tab-chats': {
            templateUrl: 'templates/chat-teacherLesson.html',
            controller: 'chatTeacherLessonCtrl'
          }
        }
      })

      .state('tab.chat-addLesson', {
        url: '/chats/{chatId:5}',
        views: {
          'tab-chats': {
            templateUrl: 'templates/chat-addLesson.html',
            controller: 'chatAddLessonCtrl'
          }
        }
      })

      .state('tab.chat-salary', {
        url: '/chats/{chatId:6}',
        views: {
          'tab-chats': {
            templateUrl: 'templates/chat-salary.html',
            controller: 'chatSalaryCtrl'
          }
        }
      })

      .state('tab.chat-active', {
        url: '/chats/{chatId:7}',
        views: {
          'tab-chats': {
            templateUrl: 'templates/chat-active.html',
            controller: 'chatActiveCtrl'
          }
        }
      })

      .state('tab.chat-detail', {
        url: '/chats/{chatId:[0-1]}',
        views: {
          'tab-chats': {
            templateUrl: 'templates/chat-detail.html',
            controller: 'ChatDetailCtrl'
          }
        }
      })

      .state('tab.account', {
        url: '/account',
        views: {
          'tab-account': {
            templateUrl: 'templates/tab-account.html',
            controller: 'AccountCtrl'
          }
        }
      })

      .state('tab.user', {
        url: '/userInfo',
        views: {
          'tab-user': {
            templateUrl: 'templates/tab-user.html',
            controller: 'userInfoCtrl'
          }
        }
      })

      .state('tab.user-changePwd', {
        url: '/userInfo/{menuId:11}',
        views: {
          'tab-user': {
            templateUrl: 'templates/user-changePwd.html',
            controller: 'userChangePwd'
          }
        }
      })

      .state('tab.user-info',{
        url:'/userInfo/{menuId:0}',
        views:{
          'tab-user':{
            templateUrl:'templates/user-info.html',
            controller:'userInfoDetail'
          }
        }
      });


    // if none of the above states are matched, use this as the fallback
    $urlRouterProvider.otherwise('/tab/dash');

  });
